//
//  AYuStatusPictureView.swift
//  SHWeibo3
//
//  Created by 余伟康 on 16/7/25.
//  Copyright © 2016年 wangce. All rights reserved.
//

import UIKit
// cell 可重用表示符
private let AYUStatusPictureViewCellId = "AYUStatusPictureViewCell"
// cell间距
let cellMargin: CGFloat = 5
// cell宽高
let cellWH: CGFloat = (UIScreen.mainScreen().bounds.width - 5*cellMargin - 10)/3

class AYuStatusPictureView: UICollectionView {
    
    // 创建一个模型
    var pictureInfo:[AYuPictureInfo]?{
        didSet{
            // countLabel 赋值
            countLabel.text = "\(pictureInfo?.count ?? 0)"
            // 获得当前配图的size
            let size = getPictureViewSize(pictureInfo?.count ?? 0)
            self.snp_updateConstraints{ (make) -> Void in
                make.size.equalTo(size)
            }
            // 刷新UI
            reloadData()
        }
    }
    
    // 计算配图的size
    func getPictureViewSize(count: Int) -> CGSize{
        
        // 计算出行数和列数
        let col = count == 4 ? 2 : count >= 3 ? 3 : count
        let row = count == 4 ? 2 : (count - 1)/3 + 1
        // 计算size 中的宽和高
        let w = CGFloat(col) * cellWH + CGFloat(col - 1)*cellMargin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1)*cellMargin
        
        
        return CGSize(width: w, height: h)
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        backgroundColor = UIColor.clearColor()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        // 获取layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        // 设置itemSize
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        // 设置水平和垂直间距
        layout.minimumInteritemSpacing = 5
        layout.minimumLineSpacing = 5
        // 注册cell
        registerClass(AYuStatusPictureViewCell.self, forCellWithReuseIdentifier: AYUStatusPictureViewCellId)
        // 设置数据源代理
        dataSource = self
        // 添加控件
        addSubview(countLabel)
        // 添加约束
        countLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
    }
    // 懒加载
    private lazy var countLabel:UILabel = UILabel(textColor: UIColor.redColor(), textFont: 35)

}


extension AYuStatusPictureView:UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureInfo?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(AYUStatusPictureViewCellId, forIndexPath: indexPath) as! AYuStatusPictureViewCell
        cell.pic_url = pictureInfo![indexPath.item]
        return cell
    }
}


// 自定义cell
class AYuStatusPictureViewCell:UICollectionViewCell{
    var pic_url : AYuPictureInfo?{
        didSet{
            // 设置image
            bgImageView.sh_setImage(pic_url?.thumbnail_pic, placeholderImgName: "weibo_placeholder")
            // 判断是否为nil 而且后缀为gif(考虑cell的重用问题)
            if let thPic = pic_url?.thumbnail_pic where thPic.hasSuffix(".gif"){
                gifImageView.hidden = false
            }else{
                gifImageView.hidden = true
            }
        }
    }
    
    override init(frame:CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置视图
    private func setupUI(){
        backgroundColor = RandomColor()
        // 添加控件 
        contentView.addSubview(bgImageView)
        contentView.addSubview(gifImageView)
        // 添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
    }
    
    // 懒加载控件
    private lazy var bgImageView:UIImageView = {
        let img = UIImageView(imgName: "weibo_placeholder")
        img.contentMode = .ScaleAspectFill
        img.layer.masksToBounds = true
        return img
    }()
    
    // 动态图片logo
    private lazy var gifImageView : UIImageView = UIImageView(imgName: "timeline_image_gif")
}
























